﻿#include <QTime>
#include "CPdfRptRestOper.h"
#include "CGlobal.h"
#include "CPdfRptBase.h"
#include "cimagedownloader.h"

CPdfRptRestOper::CPdfRptRestOper(CPdfRptTool *pPdfRptTool, QObject *parent)
	: QObject(parent)
{
	m_pPdfRptTool = pPdfRptTool;
}

CPdfRptRestOper::~CPdfRptRestOper()
{

}

bool CPdfRptRestOper::drawRecOverviewForm(double &dStartRow, double &dStartCol, CAllPdfRptDataInfo &dataInfo, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (m_pPdfRptTool == nullptr)
	{
		return false;
	}

	//判断是否需要换页画
	int nNeedRows = 6;
	m_pPdfRptTool->judgeExecAutoPageChg(dStartRow, dStartCol, nNeedRows, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev24Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 4, 2, A2T("皮带跑偏检测"), painter, printer, sErrMsg);//绘制表格附带文字-->表名称(皮带纠偏)

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev18, enPdfRptPenBlack);//设置字体和画笔等级

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测日期"), painter, printer, sErrMsg);//绘制表格附带文字-->检测日期
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 3, 1, dataInfo.rectifyDataInfo.m_sDetTime, painter, printer, sErrMsg);//绘制表格附带文字-->检测日期

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("报警项数"), painter, printer, sErrMsg);//绘制表格附带文字-->报警项数
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 3, 1, QString::number(dataInfo.rectifyDataInfo.m_nAlmCount), painter, printer, sErrMsg);//绘制表格附带文字-->报警项数

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测开始时间"), painter, printer, sErrMsg);//绘制表格附带文字-->检测开始时间
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, dataInfo.rectifyDataInfo.m_sDetStartTime, painter, printer, sErrMsg);//绘制表格附带文字-->检测开始时间

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测结束时间"), painter, printer, sErrMsg);//绘制表格附带文字-->检测结束时间
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, dataInfo.rectifyDataInfo.m_sDetEndTime, painter, printer, sErrMsg);//绘制表格附带文字-->检测结束时间

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("备注"), painter, printer, sErrMsg);//绘制表格附带文字-->备注
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 3, 1, dataInfo.rectifyDataInfo.m_sComment, painter, printer, sErrMsg);//绘制表格附带文字-->备注

	return true;
}

bool CPdfRptRestOper::drawRecAllAlmDes(double &dStartRow, double &dStartCol, CAllPdfRptDataInfo &dataInfo, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (dataInfo.rectifyDataInfo.m_vecFormRecAlmInfo.size() == 0)
	{
		return true;
	}

	//遍历Vec容器
	for (int nIndex = 0; nIndex < dataInfo.rectifyDataInfo.m_vecFormRecAlmInfo.size(); nIndex++)
	{
		if (0 == nIndex)
		{
			printer.newPage();//添加新的一页 第一个纠偏报警需强制换页开始显示
			dStartRow = 0;
			dStartCol = 0;

			//绘制详述表头
			drawRecAlmDesHeader(dStartRow, dStartCol, painter, printer, sErrMsg);
		}

		drawRecSingAlmDes(dStartRow, dStartCol, nIndex, dataInfo.rectifyDataInfo.m_vecFormRecAlmInfo[nIndex], painter, printer, sErrMsg);//绘制单个纠偏报警详情(不包含表头)
		dStartRow += 1;//间隔一行再画下一组
	}

	return true;
}

bool CPdfRptRestOper::drawRecSingAlmDes(double &dStartRow, double &dStartCol, int nAlmIdex, SPdfRecSglAlmInfo &sglAlmInfo, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (m_pPdfRptTool == nullptr)
	{
		return false;
	}

	//判断是否需要换页画
	int nNeedRows = 15;//11->15
	m_pPdfRptTool->judgeExecAutoPageChg(dStartRow, dStartCol, nNeedRows, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("序号"), painter, printer, sErrMsg);//绘制表格附带文字-->序号
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(nAlmIdex + 1), painter, printer, sErrMsg);//绘制表格附带文字-->序号

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("跑偏点名称"), painter, printer, sErrMsg);//绘制表格附带文字-->跑偏检测点名称
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.m_sRecPointName, painter, printer, sErrMsg);//绘制表格附带文字-->跑偏检测点名称

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测时间"), painter, printer, sErrMsg);//绘制表格附带文字-->跑偏开始时间
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	if (sglAlmInfo.m_sRecStartTime.isEmpty())
	{
		m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("/"), painter, printer, sErrMsg);
	}
	else
	{
		m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.m_sRecStartTime, painter, printer, sErrMsg);//绘制表格附带文字-->跑偏开始时间
	}

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测结束时间"), painter, printer, sErrMsg);//绘制表格附带文字-->跑偏结束时间
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	if (sglAlmInfo.m_sRecEndTime.isEmpty())
	{
		m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("/"), painter, printer, sErrMsg);
	}
	else
	{
		m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.m_sRecEndTime, painter, printer, sErrMsg);//绘制表格附带文字-->跑偏结束时间
	}

	//原来基础上加上4行
	//(第一行)
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("左侧实时值(mm)"), painter, printer, sErrMsg);
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_dLeftCurVal, 'f', 2), painter, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("右侧实时值(mm)"), painter, printer, sErrMsg);
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_dRightCurVal, 'f', 2), painter, printer, sErrMsg);

	//(第二行)
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("一级报警阈值(mm)"), painter, printer, sErrMsg);
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_nReaSetValOne), painter, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("二级报警阈值(mm)"), painter, printer, sErrMsg);
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_nReaSetValTwo), painter, printer, sErrMsg);

	//(第三行)
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("左侧平均值(mm)"), painter, printer, sErrMsg);
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_dLeftAvgVal, 'f', 2), painter, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("右侧平均值(mm)"), painter, printer, sErrMsg);
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_dRightAvgVal, 'f', 2), painter, printer, sErrMsg);

	//(第四行)
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测描述"), painter, printer, sErrMsg);
	if (sglAlmInfo.m_nAlmType != 0)//报警
	{
		m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenRed);//设置字体和画笔等级
	}
	else
	{
		m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13, enPdfRptPenBlack);//设置字体和画笔等级
	}
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 3, 1, sglAlmInfo.m_sAlmDesc, painter, printer, sErrMsg);

	//绘制报警图片
	//先绘制左侧合并的单元格
	double dTempStartRow = dStartRow;
	double dTempStartCol = dStartCol;
	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev13Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dTempStartRow, dTempStartCol, 1, 9, A2T("照片"), painter, printer, sErrMsg);
	dStartCol += 1;
	//下载图片
	QString sHttpPicUrl = sglAlmInfo.m_sPictureUrl;
	CImageDownloader downPic(sHttpPicUrl);
	QPixmap pixmapNow;
	if (sHttpPicUrl.size() > 0)
	{
		downPic.getNewDlPixmap(pixmapNow);
	}
	m_pPdfRptTool->drawFormAttachPic(dStartRow, dStartCol, 3, 9, pixmapNow, painter, printer, sErrMsg);

	return true;
}

bool CPdfRptRestOper::drawRecAlmDesHeader(double &dStartRow, double &dStartCol, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (m_pPdfRptTool == nullptr)
	{
		return false;
	}

	//判断是否需要换页画
	int nNeedRows = 2;
	m_pPdfRptTool->judgeExecAutoPageChg(dStartRow, dStartCol, nNeedRows, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev24Bold, enPdfRptPenBlack);//设置字体和画笔等级
	QString sTitle = A2T("%1").arg(A2T("皮带跑偏检测"));
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 4, 2, sTitle, painter, printer, sErrMsg);//绘制表格附带文字-->表名称(皮带跑偏检测)
}
